from construct2 import *


c = Struct("foo",
    String("world", 7, paddir = "right", padchar = "Z"),
    PascalString("hello", lengthfield = ULInt16),
    CString("bar"),
)
o = c.parse("kakiZZZ\x04\x00abcdhello\x00qqqq")
print o
print c.build(o)

c = Struct("foo",
    Enum(
        UBInt32("bar"),
        BLAH1 = 1,
        BLAH3 = 3,
        BLAH8 = 8,
        BLAH11 = 11,
    ),
    FlagsEnum(UBInt16("rab"),
        f1 = 1,
        f2 = 2,
        f3 = 4,
        f4 = 8,
        f5 = 16,
        f6 = 32,
        f7 = 64,
    ),
)

o = c.parse("\x00\x00\x00\x03\x00\x0e")
print o
print c.build(o)

c = Struct("foo",
    UBInt16("a"),
    EmbeddedBitStruct(
        Flag("x"),
        BitField("y", 4),
        Padding(2),
        Flag("z")
    ),
    UBInt16("c"),
)

try:
    o = c.parse("\x00\x01" "\xf8" "\x00\x02")
except:
    import pdb, sys, traceback
    t, v, tb = sys.exc_info()
    traceback.print_exception(t, v, tb)
    print "---"
    pdb.post_mortem(tb)
else:
    print o
    print c.build(o)


c = BitStruct("foo",
    Flag("f1"),
    Flag("f2"),
    Flag("f3"),
    Padding(3),
    Flag("f4"),
    Flag("f5"),
)
print c.parse("\xc2")





































